Page({

  /**
   * 页面的初始数据
   */
  data: {
    params: {
      userId: wx.getStorageSync('userId')
    },


    list: [],

    openPop: false,


    userHeadPortraitPath: '',

    bgImgPath: 'https://timebank-prod-img.oss-cn-hangzhou.aliyuncs.com/Slx/2_sbhd/20200521135424.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getCanvas()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  //获取qrcode
  getQrcode () {
    var _this = this

    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/weChatAccount/qrCodeShare/auth',
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.success) {
          wx.hideLoading()
          _this.setData({
            qrCode: res.data.data.url
          })
          _this.downloadHeader2(_this.data.qrCode)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })

  },

  //获取用户信息
  getUserinfo(params) {
    var _this = this

    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/user/infos/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.hideLoading()
          _this.setData({
            users: res.data.data,
            userHeadPortraitPath: res.data.data.userHeadPortraitPath,
            nickName: res.data.data.name,
            bookDonateOrderNum: res.data.data.bookDonateOrderNum,
            trolleyNum: res.data.data.trolleyNum,
            sdxScores: res.data.data.sdxScores,
            userTel: res.data.data.userTel
          })
          _this.downloadHeader(_this.data.userHeadPortraitPath)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserinfo(this.params)
    this.getList()
    this.getQrcode()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  share () {
    var _this = this
      setTimeout(function () {
        _this.setData({
          openPop: true
        })
        _this.getCanvas()
      },1000)
  },


  startHua() {
    console.log(111111111)
    this.setData({
      isOpen: false,
      openPop: false
    })
  },
  touchMove() {
    this.setData({
      isOpen: false
    })
  },

  saveImg() {
    // debugger
    var that = this
    wx.saveImageToPhotosAlbum({
      filePath: that.data.downImg,
      success(res) {
        wx.showToast({
          title: '保存成功'
        })
      }
    })
  },


  getCanvas () {
    var that = this
    wx.showLoading({
      title: '图片生成中...',
      icon: 'loading'
    })
    wx.getImageInfo({
      src: that.data.bgImgPath,
      success: function (res) {
        let ctx = wx.createCanvasContext('shareCanvas', that)

        let W = wx.getSystemInfoSync().windowWidth



        ctx.drawImage(res.path, 0, 0, wx.getSystemInfoSync().windowWidth * 0.9, wx.getSystemInfoSync().windowHeight * 0.77)

        ctx.save()


        // ctx.beginPath()

        ctx.arc((wx.getSystemInfoSync().windowHeight * 0.08) / 2 + (W * 0.8 / 8), (wx.getSystemInfoSync().windowHeight * 0.08) / 2 + wx.getSystemInfoSync().windowHeight * 0.05, (wx.getSystemInfoSync().windowHeight * 0.08) / 2, 0, Math.PI * 2, false)

        ctx.clip()

        ctx.drawImage(that.data.shareHeader1, W * 0.8 / 8, wx.getSystemInfoSync().windowHeight * 0.05, wx.getSystemInfoSync().windowHeight * 0.08, wx.getSystemInfoSync().windowHeight * 0.08)

        ctx.restore()
        ctx.setStrokeStyle('white')

        ctx.save()


        // 昵称

        ctx.setFontSize(15)
        ctx.setFillStyle('#FFFFFF')
        ctx.fillText(that.data.nickName, 90, wx.getSystemInfoSync().windowHeight * 0.08)


        ctx.setFontSize(12)
        ctx.setFillStyle('#FFFFFF')
        ctx.fillText("我发现了一个能便宜买书的好地方", 90, wx.getSystemInfoSync().windowHeight * 0.12)



        ctx.drawImage(that.data.shareHeader2, W * 0.34, wx.getSystemInfoSync().windowHeight * 0.54, wx.getSystemInfoSync().windowHeight * 0.14, wx.getSystemInfoSync().windowHeight * 0.14)


        ctx.restore()
        ctx.setStrokeStyle('white')

        ctx.save()




        ctx.setFontSize(12)
        ctx.setFillStyle('#FFFFFF')
        ctx.fillText("长按识别扫码买书", (W - ctx.measureText('长按识别扫码买书').width) / 2.3, wx.getSystemInfoSync().windowHeight * 0.73)

        // ctx.setFontSize(12)
        // ctx.setFillStyle('#FFE2E4')
        // ctx.fillText("限时内测过期不侯", (W - ctx.measureText("限时内测过期不侯").width) / 2.32, wx.getSystemInfoSync().windowHeight * 0.6)



        // ctx.setFontSize(12)
        // ctx.setFillStyle('#FFFFFF')
        // ctx.fillText("长按识别扫码买书", (W - ctx.measureText("长按识别扫码买书").width) / 2.32, wx.getSystemInfoSync().windowHeight * 0.76)




        // ctx.beginPath()

        // ctx.arc((wx.getSystemInfoSync().windowHeight * 0.1) / 2 + (W * 0.8 / 2.2), (wx.getSystemInfoSync().windowHeight * 0.1) / 2 + wx.getSystemInfoSync().windowHeight * 0.62, (wx.getSystemInfoSync().windowHeight * 0.1) / 2, 0, Math.PI * 2, false)

        // ctx.clip()

        // ctx.drawImage(that.data.shareHeader2, W * 0.8 / 2.2, wx.getSystemInfoSync().windowHeight * 0.62, wx.getSystemInfoSync().windowHeight * 0.1, wx.getSystemInfoSync().windowHeight * 0.1)

        // ctx.restore()
        // ctx.setStrokeStyle('white')

        // ctx.save()


        // ctx.stroke()

        ctx.draw(true, setTimeout(function () {
          that.drawPicture()
        }, 300))
      }
    })
  },

  drawPicture() {
    var that = this
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: wx.getSystemInfoSync().windowWidth * 0.9,
      height: wx.getSystemInfoSync().windowHeight * 0.77,
      quality: 1,
      destWidth: (wx.getSystemInfoSync().windowWidth * 0.9) * 2,  //输出的图片的宽度（写成width的两倍，生成的图片则更清晰）
      destHeight: (wx.getSystemInfoSync().windowHeight * 0.77) * 2,
      canvasId: 'shareCanvas',
      success: function (res) {
        wx.hideLoading()
        that.setData({
          downImg: res.tempFilePath
        })
        console.log('1111111' + that.data.downImg)
      },
      fail: function (err) {
        console.log(err)
      }
    }, this)
  },


  downloadHeader(url) {
    var that = this
    wx.downloadFile({
      url: url,
      success: function (res) {
        that.setData({
          shareHeader1: res.tempFilePath
        })
      }
    })
  },


  downloadHeader2(url) {
    var that = this
    wx.downloadFile({
      url: url,
      success: function (res) {
        that.setData({
          shareHeader2: res.tempFilePath
        })
      }
    })
  },

  onShareAppMessage(res) {
    if (res.from === 'button') {
      // let shareInfo = res.target.dataset.shareinfo;
      // console.log(shareInfo.imageUrl)
      return {
        title: "原价收书,二折买书",
        path: "/pages/index/index?userIds=" + wx.getStorageSync("userId"),
        imageUrl: "https://timebank-prod-img.oss-cn-hangzhou.aliyuncs.com/Slx/2_sbhd/sdhd_4.png"
      }
    }
  },


  //获取我的邀请列表
  getList () {
    var _this = this
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/scene/findInviter/auth',
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.success) {
          wx.hideLoading()
          _this.setData({
            list: res.data.data
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  // getCanvas () {
  //   wx.getImageInfo({
  //     src: 'https://timebank-prod-img.oss-cn-hangzhou.aliyuncs.com/Slx/2_sbhd/20200521135424.png',
  //     success: function (res) {
  //       let ctx = wx.createCanvasContext('shareCanvas', that)

  //       let W = wx.getSystemInfoSync().windowWidth

  //       ctx.drawImage(res.path, 0, 0, wx.getSystemInfoSync().windowWidth * 0.8, wx.getSystemInfoSync().windowHeight * 0.8)

  //       ctx.save()
  //     }
  //   })
  // }
})
